package com.clinbrain;

import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlNullable;
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.type.StandardTypes;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

import java.util.*;

/**
 * 提取关键字
 */
public final class KeywordExtract {

    private static final String APPLICATION_NUMBER = "application_number";
    private static final String TITLE = "title";
    private static final String ABSTRACT = "abstract";
    private static final String KEYWORD_EXTRACT = "keyword_extract";
    private static final String LANG_TYPE = "lang_type";


    private KeywordExtract() {

    }

    @ScalarFunction("keyword_extract")
    @Description("keyword_extract")
    @SqlNullable
    @SqlType("varchar")
    public static Slice keywordExtract(@SqlType(StandardTypes.VARCHAR) Slice applicationNumber,
                                       @SqlType(StandardTypes.VARCHAR) Slice title,
                                       @SqlType(StandardTypes.VARCHAR) Slice abstracts,
                                       @SqlType(StandardTypes.VARCHAR) Slice keywordExtract,
                                       @SqlType(StandardTypes.VARCHAR) Slice langType,
                                       @SqlType(StandardTypes.VARCHAR) Slice url) {

        String res = null;

//        if (null == applicationNumber || 0 == applicationNumber.length()) {
//            throw new RuntimeException("The application_number cannot be null");
//        }
//
//        if (null == title || 0 == title.length()) {
//            throw new RuntimeException("The title cannot be null");
//        }
//
        if (null == abstracts || 0 == abstracts.length()) {
            throw new RuntimeException("The abstract cannot be null");
        }

        if (null == langType || 0 == langType.length()) {
            throw new RuntimeException("The abstract cannot be null");
        }
//
//        if (null == keywordExtract || 0 == keywordExtract.length()) {
//            throw new RuntimeException("The keyword_extract cannot be null");
//        }

        if (null == url || 0 == url.length()) {
            throw new RuntimeException("The request url cannot be null");
        }

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put(APPLICATION_NUMBER, applicationNumber.toStringUtf8());
        paramMap.put(TITLE, title.toStringUtf8());
        paramMap.put(ABSTRACT, abstracts.toStringUtf8());
        paramMap.put(KEYWORD_EXTRACT, keywordExtract.toStringUtf8());
        paramMap.put(LANG_TYPE, langType.toStringUtf8());
        try {
            res = HttpTool.doPost(url.toStringUtf8(), paramMap);
            return Slices.utf8Slice(res);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(keywordExtract(
                Slices.utf8Slice(""),
                Slices.utf8Slice(""),
                Slices.utf8Slice("一种用于治疗脑病的外用膏剂及其制备方法。外用膏剂是将由中药组合物、DHA、神经酸、维生素B和瓜尔豆胶组成的混合物经过水煮而制成；中药组合物包括当归、三七、丹参、远志、川芎、石菖蒲和薄荷；当归、三七、丹参、远志、川芎、石菖蒲、薄荷、DHA、神经酸和维生素B的重量比为1‑30∶1‑30∶1‑30∶1‑30∶1‑30∶1‑30∶1‑6∶1‑6∶1‑6∶1‑6；中药组合物、DHA、神经酸和维生素B的总重量与瓜尔豆胶的重量比为100:5—30。本发明效果：使用简单方便，费用较低，不伤患者的脾胃肝肾；主要成份为中药，药效可通过患者头部的太阳穴、百会穴，颈部静脉窦等部位经透皮吸收直达脑部病灶，具有改善脑循环、脑供血、脑功能，活血化瘀、开窍醒脑之功效；可工业化批量生产，简单易行，成本较低。"),
                Slices.utf8Slice(""),
                Slices.utf8Slice("cn"),
                Slices.utf8Slice("http://10.10.9.82:8989/api")).toStringUtf8());
    }
}
